﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Data.SqlClient;
using System.Data.EntityClient;
using VLUSM_BusinessLogic.Helpers;
using VLUSM_DataAccess;
using VLUSM_BusinessLogic.DataTransferObjects;
using System.Data;
using System.Collections.ObjectModel;

namespace VLUSM_BusinessLogic
{
    public class UserAccountManager
    {
        IUnityContainer container;

        EntityConnection mainDataConnection;
        EntityConnection DVHCConnection;
        EntityConnection diemConnection;

        public UserAccountManager(IUnityContainer container)
        {
            this.container = container;
            this.Initialize();
        }

        public UserDTO GetUserByID(int IDFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.User.Single(u => u.ID == IDFilter);
                return Converter.ConvertToUserDTO(result);
            }
        }

        public UserDTO GetUserByTenTaiKhoan(string TenTaiKhoanFilter)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.User.Single(u => u.TenTaiKhoan == TenTaiKhoanFilter);
                return Converter.ConvertToUserDTO(result);
            }
        }

        public UserDTO GetUserByUserNamePassword(string username, string password)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.User.Single(u => (u.TenTaiKhoan.Equals(username) && u.MatKhau.Equals(password)));
                if (result.IsLocked != true)
                {
                    var user = Converter.ConvertToUserDTO(result);
                    var donViList = new ObservableCollection<DonVi>(context.DonVi.Where(d => d.Users.Any(u => u.ID == user.ID)));
                    ObservableCollection<Quyen> fullQuyenList = new ObservableCollection<Quyen>();
                    foreach (var donVi in donViList)
                    {
                        var quyenList = context.Quyen.Where(q => q.DonVis.Any(d => d.ID == donVi.ID)).ToList();
                        foreach (var quyen in quyenList)
                        {
                            fullQuyenList.Add(quyen);
                        }
                    }
                    user.DanhSachDonVi = Converter.ConvertToDonViDTOList(donViList);
                    user.DanhSachQuyen = Converter.ConvertToQuyenDTOList(fullQuyenList);
                    return user;
                }
                else { throw new Exception(); };
            }
        }

        public void UpdateUserAccount(UserDTO objdto)
        {
            var obj = Converter.ConvertToUser(objdto);
            using (var context = container.Resolve<MainDataContainer>())
            {
                context.User.Attach(obj);
                    context.ObjectStateManager.ChangeObjectState(obj, EntityState.Modified);
                context.SaveChanges();  
            }
        }

        public void ChangePassword(int ID,string password)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var user = context.User.Single(u => u.ID == ID);
                user.MatKhau = password;
                context.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public bool Validate(string username, string password)
        {
            using (var context = container.Resolve<MainDataContainer>())
            {
                var result = context.User.Single(u => (u.TenTaiKhoan == username && u.MatKhau == password));
                if (result == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        #region

        private void Initialize()
        {
            DVHCConnection = EntityConnectionCreator.Instance.DonViHanhChinhConnection;
            mainDataConnection = EntityConnectionCreator.Instance.MainDataConnection;
            diemConnection = EntityConnectionCreator.Instance.DiemConnection;

            this.UnityRegisterType();
            this.InitializeMapper();
        }

        private void UnityRegisterType()
        {
            container.RegisterType<MainDataContainer>(new InjectionConstructor(mainDataConnection));
        }

        private void InitializeMapper()
        {
            Converter.ConfigureUserMapping();
            Converter.ConfigureQuyenMapping();
            Converter.ConfigureDonViMapping();
        }
        #endregion
    }
}
